package com.zx.util;


import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获得软件基本信息从网络中
 */
public class TfwInfoFromIneter {

    private static final Logger LOGGER = LoggerFactory.getLogger(TfwInfoFromIneter.class);

    public static void main(String[] args) throws Exception {
        String remark = getRemark("dubbo");
        System.out.println(remark);
    }

    public static String getRemark(String work) throws Exception {
        String url = "https://baike.baidu.com/search/word?word=" + work;
        try {
            Connection connect = Jsoup.connect(url);
            // 模拟浏览器请求
            connect.header("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
            connect.header("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            connect.header("Accept-Language", "zh-CN,zh;q=0.9");
            connect.header("Connection", "keep-alive");
            // 执行URL
            Response response = connect.execute();

            int statusCode = response.statusCode();
            LOGGER.info("响应值={}", statusCode);
            if (statusCode != 200 && statusCode != 302) {
                return "";
            }

            String body = response.charset("UTF-8").body();

            Document doc = Jsoup.parse(body);
			
			/*Elements select = doc.select(".main-content");

	        Element ele = select.get(0);

	        ele.select(".top-tool").remove().get(0);*/

            Elements select = doc.select(".lemma-summary");
            Element ele = select.get(0);

            return ele.toString();

        } catch (Exception e) {
            LOGGER.error("解析URL出现异常,url={}", url);
        }
        return "";
    }

}
